<?php
/**
 * Created by PhpStorm.
 * User: Mak
 * Date: 2016/4/12 0012
 * Time: 20:36
 */

namespace Admin\Controller;

use Think\Controller;
use Think\Page;

//比赛管理
class MatchMagController extends Controller
{
    //所有比赛
    public function allMatch()
    {
        $p = empty($_GET['p']) ? 1 : $_GET['p'];
        $enrollinfo = D("EnInfo");
        $ret = $enrollinfo->order("addtime desc")->where("issue=1")->page($p . ',10')->select();

        $count = $enrollinfo->where("issue=1")->count();
        $Page = new Page($count, 10);
        $Page->setConfig('prev', '<li class="waves-effect"><i class="mdi-navigation-chevron-left"></i></li>');
        $Page->setConfig('next', '<li class="waves-effect"><i class="mdi-navigation-chevron-right"></i></li>');
        $Page->setConfig('first', '<li class="waves-effect"><i class="material-icons">1</i></li>');
        $Page->setConfig('last', '<li class="waves-effect"><i class="material-icons">%TOTAL_PAGE%</i></li>');
        $Page->setConfig('theme', '%UP_PAGE% %FIRST% %LINK_PAGE% %DOWN_PAGE% %END%');
        $show = $Page->show();
        $this->assign('page', $show);

        $this->assign("ret", $ret);
        $this->assign("count", $count);

        $this->display();
    }

    //导出赛项信息
    public function output()
    {
        import("Org.Util.PHPExcel");
        $enrollinfo = D("EnInfo");
        $ret = $enrollinfo->order("addtime desc")->where("issue=1")->select();
        $PHPExcel = new \PHPExcel();
        $Sheet = $PHPExcel->getActiveSheet();
        $Sheet->getDefaultStyle()->getFont()->setName("宋体")->setSize(14);
        $Sheet->getDefaultStyle()
            ->getAlignment()
            ->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER)
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $Sheet->getColumnDimension('A')->setWidth(5);
        $Sheet->getColumnDimension('B')->setAutoSize(true);
        $Sheet->getColumnDimension('C')->setWidth(7);
        $Sheet->getColumnDimension('D')->setWidth(60);
        $Sheet->getColumnDimension('E')->setWidth(20);
        $Sheet->getColumnDimension('F')->setWidth(20);
        $Sheet->getColumnDimension('G')->setAutoSize(true);
        $Sheet->getColumnDimension('H')->setAutoSize(true);
        $Sheet->getColumnDimension('I')->setAutoSize(true);
        $Sheet->setCellValue("A1", "序号")
            ->setCellValue("B1", "赛项编号")
            ->setCellValue("C1", "类别")
            ->setCellValue("D1", "赛项名称")
            ->setCellValue("E1", "系部名称")
            ->setCellValue("F1", "最大报名人数")
            ->setCellValue("G1", "报名开始时间")
            ->setCellValue("H1", "报名截止时间")
            ->setCellValue("I1", "添加时间")
            ->getStyle("A1:I1")
            ->getFont()
            ->setName("微软雅黑")
            ->setSize(14)
            ->setBold(true);
        $startcell = 2;
        $i = 1;
        foreach ($ret as $rows) {
            $Sheet->getStyle("B" . $startcell)->getNumberFormat()->setFormatCode(\PHPExcel_Style_NumberFormat::FORMAT_TEXT);
            $Sheet->setCellValue("A" . $startcell, $i++)
                ->setCellValueExplicit("B" . $startcell, $rows["enid"], \PHPExcel_Cell_DataType::TYPE_STRING)
                ->setCellValue("C" . $startcell, getMatchClass($rows["enclass"]))
                ->setCellValue("D" . $startcell, $rows["title"])
                ->setCellValue("E" . $startcell, getDeptName($rows["deptid"]))
                ->setCellValue("F" . $startcell, $rows["num"])
                ->setCellValue("G" . $startcell, date("Y-m-d", $rows["start"]))
                ->setCellValue("H" . $startcell, date("Y-m-d", $rows["end"]))
                ->setCellValue("I" . $startcell, date("Y-m-d h:i:s", $rows["addtime"]));
            $startcell++;
        }

        $obj = \PHPExcel_IOFactory::createWriter($PHPExcel, "Excel5");
        browser_export("赛项信息表");
        $obj->save("php://output");
    }

    //本系比赛
    public function deptMatch()
    {
        $p = empty($_GET['p']) ? 1 : $_GET['p'];
        $enrollinfo = D("EnInfo");

        $map["issue"] = 1;
        $map["deptid"] = session("DeptId");
        $ret = $enrollinfo->order("addtime desc")->where($map)->page($p . ',10')->select();

        $count = $enrollinfo->where($map)->count();
        $Page = new Page($count, 10);
        $Page->setConfig('prev', '<li class="waves-effect"><i class="mdi-navigation-chevron-left"></i></li>');
        $Page->setConfig('next', '<li class="waves-effect"><i class="mdi-navigation-chevron-right"></i></li>');
        $Page->setConfig('first', '<li class="waves-effect"><i class="material-icons">1</i></li>');
        $Page->setConfig('last', '<li class="waves-effect"><i class="material-icons">%TOTAL_PAGE%</i></li>');
        $Page->setConfig('theme', '%UP_PAGE% %FIRST% %LINK_PAGE% %DOWN_PAGE% %END%');
        $show = $Page->show();
        $this->assign('page', $show);

        $this->assign("ret", $ret);
        $this->display();
    }

    //我发布的比赛
    public function issueMatch()
    {
        $p = empty($_GET['p']) ? 1 : $_GET['p'];
        $enrollinfo = D("EnInfo");

        $map["addpri"] = session("UserId");
        $map["issue"] = 1;
        $ret = $enrollinfo->where($map)->order("addtime desc")->page($p . ',10')->select();

        $count = $enrollinfo->where($map)->count();
        $Page = new Page($count, 10);
        $Page->setConfig('prev', '<li class="waves-effect"><i class="mdi-navigation-chevron-left"></i></li>');
        $Page->setConfig('next', '<li class="waves-effect"><i class="mdi-navigation-chevron-right"></i></li>');
        $Page->setConfig('first', '<li class="waves-effect"><i class="material-icons">1</i></li>');
        $Page->setConfig('last', '<li class="waves-effect"><i class="material-icons">%TOTAL_PAGE%</i></li>');
        $Page->setConfig('theme', '%UP_PAGE% %FIRST% %LINK_PAGE% %DOWN_PAGE% %END%');
        $show = $Page->show();
        $this->assign('page', $show);


        $this->assign("ret", $ret);
        $this->display();
    }

    //未发布的比赛
    public function unissueMatch()
    {
        $p = empty($_GET['p']) ? 1 : $_GET['p'];
        $enrollinfo = D("EnInfo");

        $map["addpri"] = session("UserId");
        $map["issue"] = 0;
        $ret = $enrollinfo->where($map)->order("addtime desc")->page($p . ',10')->select();

        $count = $enrollinfo->where($map)->count();
        $Page = new Page($count, 10);
        $Page->setConfig('prev', '<li class="waves-effect"><i class="mdi-navigation-chevron-left"></i></li>');
        $Page->setConfig('next', '<li class="waves-effect"><i class="mdi-navigation-chevron-right"></i></li>');
        $Page->setConfig('first', '<li class="waves-effect"><i class="material-icons">1</i></li>');
        $Page->setConfig('last', '<li class="waves-effect"><i class="material-icons">%TOTAL_PAGE%</i></li>');
        $Page->setConfig('theme', '%UP_PAGE% %FIRST% %LINK_PAGE% %DOWN_PAGE% %END%');
        $show = $Page->show();
        $this->assign('page', $show);

        $this->assign("ret", $ret);
        $this->display();
    }

    //创建比赛
    public function addMatch()
    {
        if (IS_POST) {
            $post['title'] = I("post.title");
            $post['enclass'] = I("post.enclass");
            $post['pri'] = I("post.pri");
            $post['deptid'] = I("post.deptid");
            $post['addpri'] = session("UserId");
            $post['content'] = I("post.content", '', false);
            $post['num'] = I("post.num");
            $post['start'] = strtotime(I("post.start"));
            $post['end'] = strtotime(I("post.end"));
            $post['addtime'] = time();
            $post['enid'] = SetEnId($post['deptid'], session("UserId"));
            $issue = I("post.issue");
            $unissue = I("post.unissue");
            if (!empty($issue)) {
                $post['issue'] = 1;
            } elseif (!empty($unissue)) {
                $post['issue'] = 0;
            }

            $EnrollInfo = M("enrollinfo");
            //dump($post);
            if ($EnrollInfo->add($post)) {
                if ($post['issue'] == 1) {
                    $this->success("创建成功", U('Admin/MatchMag/issueMatch'));
                } else {
                    $this->success("创建成功", U('Admin/MatchMag/unissueMatch'));
                }
            }
        } else {
            $this->display();
        }
    }

    //修改比赛
    public function editMatch()
    {
        $EnrollInfo = D("EnInfo");
        $edit = I("post.edit");
        if (!empty($edit)) {
            $EnrollInfo->create();
            if ($EnrollInfo->save()) {
                $this->success("修改成功");
            } else {
                $this->error("修改失败");
            }
        } else {
            $enid = I("get.enid");
            $ret = $EnrollInfo->where("enid={$enid}")->find();
            $this->assign("ret", $ret);
//            dump($ret);
            $this->display();
        }
    }

    //发布比赛
    public function issue()
    {
        $enid = I("get.enid");
        $EnrollInfo = D("EnInfo");
        if ($ret = $EnrollInfo->where("enid={$enid}")->setField("issue", 1)) {
            redirect(U("Admin/MatchMag/allMatch"));

        } else {
            $this->error("发布成功");
        }
    }

    //取消发布比赛
    public function unissue()
    {
        $enid = I("get.enid");
        $EnrollInfo = D("EnInfo");
        if ($ret = $EnrollInfo->where("enid={$enid}")->setField("issue", 0)) {
            redirect(U("Admin/MatchMag/allMatch"));

        } else {
            $this->error("取消失败");
        }
    }

    //删除比赛
    public function delete()
    {
        $enid = I("get.enid");
        $EnrollInfo = D("EnInfo");
        if ($EnrollInfo->where("enid={$enid}")->delete()) {
            $this->success("删除成功");
        } else {
            $this->error("删除失败");
        }
    }
}